import request from '@/utils/request'

/**
 * 通用下载方法
 * @param {String} url 请求地址
 * @param {Object} params 请求参数
 * @param {String} filename 文件名
 */
export function download(url, params, filename) {
  return request({
    url: url,
    method: 'post',
    data: params,
    responseType: 'blob'
  }).then(res => {
    const blob = new Blob([res])
    if ('download' in document.createElement('a')) {
      const link = document.createElement('a')
      link.download = filename
      link.href = URL.createObjectURL(blob)
      link.click()
      URL.revokeObjectURL(link.href)
    } else {
      navigator.msSaveBlob(blob, filename)
    }
  })
} 